package com.nowcoder.community.entity;

public class Page {

    // 当前页码
    private int current = 1;

    // 每页的数量
    private int limit = 10;

    // 数据总行数
    private int rows = 0;

    // 访问路径，便于分页时复用
    private String path;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current > 0) {
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit > 0 && limit <= 100) {
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if (rows > 0) {
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    // 获取起始行数
    public int getOffset() {
        return limit * (current - 1);
    }

    // 获取总页数
    public int getTotal() {
        if (rows % limit == 0) {
            return rows / limit;
        } else {
            return rows / limit + 1;
        }
    }

    // 获取起始页码
    public int getFrom() {
        return Math.max(current - 2, 1);
    }

    // 获取终止页码
    public int getTo() {
        return Math.min(current + 2, this.getTotal());
    }
}
